package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecPartyLogEditBean;
import cn.ffcs.uec.staff.model.UecPartyLog;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecPartyLogEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecPartyLogEditBean bean = new UecPartyLogEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加政治面貌
	 */
	private UecPartyLog uecPartyLog;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecPartyLogEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		//政治面貌及党籍情况 partyName
		List<NodeVo> nodeVoPartyNameList = new ArrayList<NodeVo>();
		String[][] partyName = { 
				{"1001A1100000000006D6","中国共产党党员"},
				{"1001A1100000000006D7","中国共产党预备党员"},
				{"1001A1100000000006D8","中国共产主义青年团团员"},
				{"1001A1100000000006D9","中国国民党革命委员会会员"},
				{"1001A1100000000006DA","中国民主同盟盟员"},
				{"1001A1100000000006DB","中国民主建国会会员"},
				{"1001A1100000000006DC","中国民主促进会会员"},
				{"1001A1100000000006DD","中国农工民主党党员"},
				{"1001A1100000000006DE","中国致公党党员"},
				{"1001A1100000000006DF","九三学社社员"},
				{"1001A1100000000006DG","台湾民主自治同盟盟员"},
				{"1001A1100000000006DH","无党派民主人士	"},
				{"1001A1100000000006DI","群众"}};
		for (int i = 0; i < partyName.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(partyName[i][j]);
				nodeVo.setName(partyName[i][j + 1]);
			}
			nodeVoPartyNameList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPartyName(), nodeVoPartyNameList);

	}

	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecPartyLogEditWindow().setTitle("新增政治面貌信息");
			bean.getUecPartyLogEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecPartyLogEditWindow().setTitle("查看政治面貌信息");
			uecPartyLog = (UecPartyLog) arg.get("uecPartyLog");
			if (null != uecPartyLog) {
				PubUtil.fillBeanFromPo(uecPartyLog, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecPartyLogEditDiv(),true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecPartyLogEditWindow().setTitle("编辑政治面貌信息");
			bean.getUecPartyLogEditDiv().setHeight("30%");
			uecPartyLog = (UecPartyLog) arg.get("uecPartyLog");
			if (null != uecPartyLog) {
				PubUtil.fillBeanFromPo(uecPartyLog, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecPartyLog = UecPartyLog.newInstance();
			PubUtil.fillPoFromBean(bean, uecPartyLog);
			dateTransToString();
			uecPartyLog.setStaffId(uecStaffInfo.getStaffId());
			uecPartyLog.setDr(1L);
			uecPartyLog.addOnly();
			Events.postEvent("onOK", this.self, uecPartyLog);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecPartyLog);
			dateTransToString();
			uecPartyLog.updateOnly();
			Events.postEvent("onOK", this.self, uecPartyLog);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecPartyLogEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourType().getValue())) {
		// return "填写奖励类型";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourMeas().getValue())) {
		// return "填写奖励措施";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourOrg().getValue())) {
		// return "填写奖励机构";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourRank().getValue())) {
		// return "填写奖励级别";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourGrade().getValue())) {
		// return "填写奖励档次";
		// }
		return null;
	}
	/**
	 * 将Date类型时间转成String类型
	 * Date --> String
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef1().getValue())) {
			uecPartyLog.setGlbdef1(formatter.format(bean.getGlbdef1()
					.getValue()));
		} 
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef2().getValue())) {
			uecPartyLog.setGlbdef2(formatter.format(bean.getGlbdef2()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getPartyDate().getValue())) {
			uecPartyLog.setPartyDate(formatter.format(bean.getPartyDate()
					.getValue()));
		} 
		if (!StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) {
			uecPartyLog.setBeginDate(formatter.format(bean.getBeginDate()
					.getValue()));
		} 
		if (!StrUtil.isNullOrEmpty(bean.getEndDate().getValue())) {
			uecPartyLog.setEndDate(formatter.format(bean.getEndDate()
					.getValue()));
		} 
		if (!StrUtil.isNullOrEmpty(bean.getPartyDueDate().getValue())) {
			uecPartyLog.setPartyDueDate(formatter.format(bean.getPartyDueDate()
					.getValue()));
		}
	}
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 奖励日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecPartyLog.getGlbdef1()) {
			bean.getGlbdef1()
					.setValue(sdf.parse(uecPartyLog.getGlbdef1()));
		}
		if (null != uecPartyLog.getGlbdef2()) {
			bean.getGlbdef2()
					.setValue(sdf.parse(uecPartyLog.getGlbdef2()));
		}
		if (null != uecPartyLog.getPartyDate()) {
			bean.getPartyDate()
					.setValue(sdf.parse(uecPartyLog.getPartyDate()));
		}
		if (null != uecPartyLog.getBeginDate()) {
			bean.getBeginDate()
					.setValue(sdf.parse(uecPartyLog.getBeginDate()));
		}
		if (null != uecPartyLog.getEndDate()) {
			bean.getEndDate()
					.setValue(sdf.parse(uecPartyLog.getEndDate()));
		}
		if (null != uecPartyLog.getPartyDueDate()) {
			bean.getPartyDueDate()
					.setValue(sdf.parse(uecPartyLog.getPartyDueDate()));
		}
		
		
	}

}
